const path = require('path');
const express = require('express');
const { mainModule } = require('process');
const schedule = require('node-schedule');

const app = express();

const http = require('http').Server(app);
const io = require('socket.io')(http);

const staticRoot = path.join(__dirname,"web");
console.log(staticRoot);
/**
 * Init web server
 */
async function initWebServer(){
    app.use(express.static(staticRoot));
    io.on('connection', function(socket) {
        console.log('A user connected');
        //Whenever someone disconnects this piece of code executed
        socket.on('disconnect', function () {
        console.log('A user disconnected');
        });
    });

    http.listen(80,()=>{
        console.log("server start!")
    });
}
const initSchedule = async ()=>{
    console.log("init schedule");
    //每分钟的第30秒定时执行一次:
    schedule.scheduleJob('1/10 * * * * *',()=>{
        console.log('scheduleCronstyle:' + new Date());
    });
}
async function main(){
    console.log("main function.");
    initSchedule();
    initWebServer();
}
main();
